package com.kpijavatraining.module.io;

import java.io.File;
import java.io.IOException;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import com.kpijavatraining.module.Session;
import com.kpijavatraining.module.User;
import com.kpijavatraining.module.utils.WrapperList;
import com.kpijavatraining.module2.question.Question;


/**
 * Created with IntelliJ IDEA.
 * User: Yura
 * Date: 13.10.13
 * Time: 14:39
 * To change this template use File | Settings | File Templates.
 */

/**
 * This class used for loading questions from file
 */
public class LoaderFromFile
{
	private final String PATH = "Questions/";
	private WrapperList<WrapperList<Question>> list = new WrapperList<>();
   
	public Session loadQuestions(User user) throws IOException, ClassNotFoundException, CloneNotSupportedException, SAXException {
		File file = new File(PATH);
		String[] topics = file.list();
		for(String topic : topics){
			XMLReader reader = XMLReaderFactory.createXMLReader();
			SimpleHandler handler = new SimpleHandler();
			reader.setContentHandler(handler);
			reader.parse(PATH+topic);
			list.add(handler.getQuestions());	
		}
		return new Session(list,user);
	}
}

